Obafemi Emmanuel

Text Formatting in HTML

Published 1 month ago

HTML (HyperText Markup Language) provides various elements for formatting text on web pages. Proper text formatting enhances readability and improves the structure of content. In this guide, we will explore different text formatting tags in HTML, including headings, paragraphs, line breaks, bold, italics, underline, superscript, subscript, and preformatted text.


1. Headings (<h1> to <h6>)

Headings are essential for structuring content and improving SEO. HTML provides six levels of headings, from <h1> (the most important) to <h6> (the least important).


Example:

<h1>This is Heading 1</h1>
<h2>This is Heading 2</h2>
<h3>This is Heading 3</h3>
<h4>This is Heading 4</h4>
<h5>This is Heading 5</h5>
<h6>This is Heading 6</h6>

Output:

This is Heading 1

This is Heading 2

This is Heading 3

This is Heading 4

This is Heading 5

This is Heading 6

2. Paragraphs (<p>)

The <p> tag is used to define a paragraph. It automatically adds space before and after the text to separate content.


Example:

<p>This is a paragraph of text. It is commonly used to organize written content.</p>

Output:

This is a paragraph of text. It is commonly used to organize written content.


3. Line Breaks (<br>) and Horizontal Rules (<hr>)

  • <br>: Inserts a line break within text.
  • <hr>: Creates a horizontal rule (a thematic break in content).

Example:

<p>This is a line of text.<br>This text appears on a new line.</p>
<hr>
<p>This is another paragraph after a horizontal rule.</p>

Output:

This is a line of text.

This text appears on a new line.

This is another paragraph after a horizontal rule.


4. Bold, Italics, and Underline

  • <b>: Makes text bold (without adding semantic importance).
  • <strong>: Makes text bold and adds importance.
  • <i>: Makes text italic (without adding emphasis).
  • <em>: Italicizes text and conveys emphasis.
  • <u>: Underlines text.

Example:

<p><b>Bold Text</b></p>
<p><strong>Strong (Important) Text</strong></p>
<p><i>Italic Text</i></p>
<p><em>Emphasized Text</em></p>
<p><u>Underlined Text</u></p>

Output:

Bold Text

Strong (Important) Text

Italic Text

Emphasized Text

Underlined Text

5. Superscript (<sup>) and Subscript (<sub>)

  • <sup>: Displays text as a superscript (raised above the baseline).
  • <sub>: Displays text as a subscript (lowered below the baseline).

Example:

<p>Water formula: H<sub>2</sub>O</p>
<p>Square of 5: 5<sup>2</sup> = 25</p>

Output:

Water formula: H₂O

Square of 5: 5² = 25


6. Preformatted Text (<pre>)

The <pre> tag is used to display text exactly as it is written in the HTML document, including spaces and line breaks.


Example:

<pre>
This is preformatted text.
    It preserves spacing and line breaks.
</pre>

Output:

This is preformatted text.
    It preserves spacing and line breaks.

Conclusion

Using the right HTML text formatting tags helps improve readability, accessibility, and SEO. By properly structuring your content with headings, paragraphs, and formatting elements, you can create well-organized and visually appealing web pages. Start implementing these tags in your HTML projects to enhance content presentation!


Leave a Comment


Choose Colour